package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.PaymentType;
import com.atguigu.lease.web.admin.service.PaymentTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "支付方式管理")
@RequestMapping("/admin/payment")
@RestController
public class PaymentTypeController {
    @Autowired
    private PaymentTypeService paymentTypeService;

    @Operation(summary = "查询全部支付方式列表")
    @GetMapping("list")
    public Result<List<PaymentType>> listPaymentType() {
        List<PaymentType> list = paymentTypeService.lambdaQuery().eq(PaymentType::getIsDeleted, 0).list();
        return Result.ok(list);
    }

    @Transactional
    @Operation(summary = "保存或更新支付方式")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdatePaymentType(@RequestBody PaymentType paymentType) {
//        PaymentType one = paymentTypeService.lambdaQuery()
//                .eq(PaymentType::getName, paymentType.getName())
//                .eq(PaymentType::getIsDeleted, 0).one();
//        if (one != null) {
//            if (one.equals(paymentType)) {
//                return Result.fail("名称已存在");
//            }
//            // 名称已存在,说明是更新
//            boolean update = paymentTypeService.updateById(paymentType);
//            if (!update) {
//                throw new CommonException("更新失败");
//            }
//
//        } else {
//            //新增
//            boolean save = paymentTypeService.save(paymentType);
//            if (!save) {
//                throw new CommonException("保存失败");
//            }
//        }

        /**
         * 注意使用 saveOrUpdate方法 会自动判断是更新还是保存
         */
        paymentTypeService.saveOrUpdate(paymentType);

        return Result.ok();
    }

    @Transactional
    @Operation(summary = "根据ID删除支付方式")
    @DeleteMapping("deleteById")
    public Result deletePaymentById(@RequestParam Long id) {
        //带有逻辑删除所以删除属于更新操作
        paymentTypeService.removeById(id);
        return Result.ok();
    }
}















